<template>
  <div class="adminPage">
    <!-- 搜索框 -->
    <div class="search_area">
      <el-form
        :model="params"
        label-width="80px"
        style="display: flex; margin-bottom: 50px"
      >
        <el-form-item label="借阅人">
          <el-input
            clearable
            placeholder="请输入借阅人"
            v-model="params.userName"
            style="width: 200px"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item label="书籍名称">
          <el-input
            clearable
            placeholder="请输入书籍名称"
            v-model="params.realName"
            style="width: 200px"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item>
          <div class="search-buttons">
            <el-button type="warning" style="margin-left: 10px" @click="search"
              >查询</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!--表单数据-->
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{
          background: '#fafafa',
          color: '#262626',
          fontSize: '14px',
          fontWeight: '500',
          height: '10px',
        }"
      >
        <el-table-column prop="bookName" label="书籍名称" align="center">
        </el-table-column>
        <el-table-column prop="userName" label="借阅人" align="center">
        </el-table-column>
        <el-table-column prop="time" label="借阅时间" align="center">
        </el-table-column>
        <el-table-column label="归还状态" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 0 ? 'info' : 'success'">
              {{ scope.row.status === 0 ? '未归还' : '已归还'}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="returnBook(scope.row)"
              v-if="$store.state.userInfo.role === 'ROLE_STUDENT'"
              >归还</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="delReserve(scope.row)"
              v-else
            >
              <el-button slot="reference" type="warning">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="block" style="margin-top: 20px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNo"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  findAllReserves,
  deleteReserve,
  findReservesByUserId,
  updateReserve
} from "@/api/sys/reserve.js";
export default {
  name: "borrowInfo",
  data() {
    return {
      //分页查询参数
      params: {
        pageSize: 10,
        pageNo: 1,
        bookName: "", //书籍名称
        userName: "", //借阅人
      },
      tableData: [], //表单数据
      loading: "", //加载状态
      total: "", //数据条数
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    //加载借阅数据
    async loadData() {
      this.loading = true;
      try {
        const res =
          this.$store.state.userInfo.role === "ROLE_STUDENT"
            ? await findReservesByUserId(this.$store.state.userInfo.id)
            : await findAllReserves(this.params);

        if (res.code === 200) {
          this.tableData =
            this.$store.state.userInfo.role === "ROLE_STUDENT"
              ? res.data
              : res.data.list;
          this.total = res.data.total || 0;
        } else if (res.code === 10001) {
          this.$message.error(res.message);
          //清理登录信息
          this.$store.dispatch("Logout");
        }
      } finally {
        this.loading = false;
      }
    },
    //根据输入模糊查询
    search() {
      this.params.pageNo = 1;
      this.loadData();
    },
    //页码尺寸变化
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.pageNo = 1;
      this.loadData();
    },
    //当前页变化
    handleCurrentChange(pageNo) {
      this.params.pageNo = pageNo;
      this.loadData();
    },

    //归还书籍
    returnBook(data) {
      this.$confirm("该操作将归还书籍?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        data.status = 1;
        updateReserve(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("归还成功");
            this.loadData();
          }
        });
      }).catch(() => {
        this.$message.info("取消归还");
      });
    },
    //删除借阅记录
    delReserve(data) {
      this.$confirm("此操作将永久删除该借阅记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (data.status === 0) {
          this.$message.error("未归还的借阅记录不能删除");
          return;
        }
        const id = data.id;
        deleteReserve(id).then((res) => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.loadData();
          }
        });
      }).catch(() => {
        this.$message.info("取消删除");
      });
    },
  },
};
</script>


<style>
.adminPage {
  min-height: 100%;
  background-color: transparent;
}

.el-button {
  margin: 5px;
}
</style>